package ca.ualberta.cs.CLRDB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class ContentSerializer {

	
	/**
	 * extracts attributes (key-value pairs) that are saved in the annotation body and not in the AnnotationModel fields
	 * for example: this method extracts the pairs (X,378) and (NAME,NOTE858892424) from an annotation with body:
	 * <note  NAME="NOTE858892424"  Behavior="uk.ac.liv.c3connector.FabNote"  
			 X='378'  Y='418'  Width="220"  Height='100'>
	 * @param annoBody
	 * @return a map of keys to values
	 */
	public static HashMap<String,String> parseAttributes(String annoBody){
		HashMap<String,String> attr = new HashMap<String, String>();
		
		/*
		 * annoBody can for example be:
		 * <note  NAME='NOTE858892424'  Behavior='uk.ac.liv.c3connector.FabNote'  
		 * 		X='378'  Y='418'  Width='220'  Height='100'>
   			<content> salam   </content> 
   			</note>
		 */
		
		String atts_section = annoBody.split(">")[0];
		
		String[] equalSplitted = atts_section.split("=");
		
		String val = null;
		String key = null;
		String part1 = null;
		String part2 = null;
		for(int i = 0 ; i < equalSplitted.length ; i++ ){
			if(part1 == null && i+1 < equalSplitted.length){
				part1 = equalSplitted[i];
				part2 = equalSplitted[i+1];
				i++;
			}
			else{
				part2 = equalSplitted[i];
			}
			
			StringTokenizer tpart1 = new StringTokenizer(part1,",. };");
//			StringTokenizer tpart1 = new StringTokenizer(part1," ");
			key = "";
//			int numOftoks = tpart1.countTokens();
			//last token
			while(tpart1.hasMoreTokens()){
				String temp = tpart1.nextToken().trim();
				if(temp != null && temp.length() != 0)
					key = temp;
			}
//			System.out.println("key:"+key+";");
			//all except last
			val = "";
//			String[] valandAfter2 = part2.split("\"");
			String[] valandAfter = part2.split(" ");
			for(int j = 0; j < valandAfter.length ; j++){				
				val += valandAfter[j] + " ";
				if(val.trim().endsWith("\""))
					break;
			}
			if(valandAfter.length == 1 || val.equals(""))
				val = valandAfter[0];
//			val = valandAfter[0];
			
			/*StringTokenizer stringremover = new StringTokenizer(val, "' \"");
			while(stringremover.hasMoreElements()){
				String token = stringremover.nextToken();
				if(token != null && token.length() != 0){
					val = token;
					break;
				}
			}*/
			
			//remove \" s from beginning and end:
			if(val.trim().startsWith("\""))
				val = val.trim().substring(1);
			if(val.trim().endsWith("\""))
				val = val.trim().substring(0, val.trim().length()-1);
			
			/*String[] temp = val.trim().split("'");
			int a= 0;
			boolean brk = false;
			do{
				a++;
				if(a >= temp.length ){
					brk = true;
					break;
				}				
			}while(temp[a] == null || temp[a].length() == 0);
			
			if(brk)
				break;
			val = temp[a];*/
			attr.put(key, val);
			
			/*int k = 0;
			boolean br = false;
			do{
				k++;
				if(k >= valandAfter.length ){
					br = true;
					break;
				}
					
			}while( valandAfter[k] == null || valandAfter[k].length() == 0);
			
			if(br)
				break;			
			part1 = valandAfter[k];*/
			
			if(valandAfter.length == 1)
				break;
			
			part1 = valandAfter[valandAfter.length-1];
			if(part1 == null || part1.length() == 0)
				break;
			
		}
		
		return attr;
	}
	
	public static String removeAttributeFromAnnotationBody(String annobody, ArrayList<String> keysToRemove){
		
		for(String key: keysToRemove){
			//TODO implement			
		}

		return annobody;
	}
	
	public static void main(String[] args) {
		String body = "<note  NAME=\"NOTE858892424\"  Behavior=\"uk.ac.liv.c3connector.FabNote\" "+  
			 "X='378'  Y='418'  Width=\"220\"  Height='100'> "+
			"<content> salam   </content> 	</note>";
		
		HashMap<String,String> atts = ContentSerializer.parseAttributes(body);
		System.out.println("done");
		
	}
}
